/**
 * @author Christiano Milfont
 * @class
 * @extends Ext.data.DWRProxySimple O objeto DWRProxy extende de DataProxy com o mecanismo de extens�o do ExtJS
 * 
 * @classDescription  Objeto que funciona como um Proxy [ver Design pattern Proxy], que 
 * � um mecanismo de captura de dados remotos, integrado ao Framework/Engine DWR. Esse funciona sem pagina��o
 * @constructor
 * @param {Object} dwr_facade Objeto Creator do DWR que funciona como uma fachada
 * @param {Object} dwr_filter Objeto que serve como um Filtro para uma "Query By Example"
 * @param {Object} dwr_errorHandler Function personalizada para tratamento de erro do DWR
 * @since 0.1
 * @version 0.1
 * @copyright Milfont.org
 */
Ext.data.DWRProxySimple = function(dwr_facade, dwr_filter, dwr_errorHandler){
    Ext.data.DWRProxySimple.superclass.constructor.call(this);
    this.data = dwr_facade;
	this.dwr_filter = dwr_filter;
	this.dwr_errorHandler = dwr_errorHandler;
};

/**
 * @extends Ext.data.DataProxy O objeto DWRProxy extende de DataProxy com o mecanismo de extens�o do ExtJS
 */
Ext.extend(Ext.data.DWRProxySimple, Ext.data.DataProxy, {
	
	/**
     * Load data from the requested source (in this case an in-memory
     * data object passed to the constructor), read the data object into
     * a block of Ext.data.Records using the passed Ext.data.DataReader implementation, and
     * process that block using the passed callback.
     * @method load
     * @param {Object} params This parameter is not used by the MemoryProxy class.
     * @param {Ext.data.DataReader) reader The Reader object which converts the data
     * object into a block of Ext.data.Records.
     * @param {Function} callback The function into which to pass the block of Ext.data.records.
     * The function must be passed <ul>
     * <li>The Record block object</li>
     * <li>The "arg" argument from the load function</li>
     * <li>A boolean success indicator</li>
     * </ul>
     * @param {Object} scope The scope in which to call the callback
     * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
     */
    load : function(params, reader, callback, scope, arg) {
		/**
		 * Escopo "this" mapeado para a vari�vel s porque dentro do callback do DWR o escopo "this"
		 * n�o pertence a classe.
		 */
		var s = this; 
        params = params || {};
		
		if(params.filter != undefined) {
			this.dwr_filter = params.filter;
		}
		
        var result;
        try {
			this.data(this.dwr_filter, {
				callback:function(response) {
					result = reader.readRecords(response);
					callback.call(scope, result, arg, true);
				},
				errorHandler:function(a, e) {
					scope.fireEvent("loadexception", s, arg, null, e);
					s.dwr_errorHandler(a);
				},
				timeout:100000
			});
			this.dwr_total_cache = true;
        } catch(e) {
            this.fireEvent("loadexception", this, arg, null, e);
            callback.call(scope, null, arg, false);
            return;
        }
       
    }
	
});